package com.foreknow.dao.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;

import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    @Override
    public boolean register(Admin admin) throws SQLException {
        String sql = "insert into admin(username,password) values(?,?)";
        int isRight = jdbcTemplate.update(sql,admin.getUsername(),admin.getPassword());
        return isRight==1;
    }

    @Override
    public Admin login(String username, String password) {
        String sql = "select * from admin where username=? and password=?";
        EntityMapping entityMapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,entityMapping,"username","password");
        if(list.size()>0) {
            return (Admin) list.get(0);
        }
        return null;
    }
}
